using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TimeTravel.XnaLibrary.UI
{
	/// <summary>
	/// Represents a game screen.
	/// </summary>
	public class Screen
	{
		private Dictionary<string, Control> mControls;

		/// <summary>
		/// Gets or sets the currently active screen.
		/// </summary>
		public static Screen Active;

		/// <summary>
		/// Gets a dictionary of all the children controls.
		/// </summary>
		public Dictionary<string, Control> Controls
		{
			get { return mControls; }
		}

		/// <summary>
		/// Initializes a new Screen instance.
		/// </summary>
		public Screen()
		{
			mControls = new Dictionary<string, Control>();
			if (Active == null)
				Active = this;
		}

		/// <summary>
		/// Updates the screen and its child controls with the specified GameTime and MouseStates.
		/// </summary>
		/// <param name="gameTime">The GameTime that passed since the last call of this method.</param>
		/// <param name="currentState">The current mouse state.</param>
		/// <param name="previousState">The mouse state in the previous frame.</param>
		public void Update(GameTime gameTime, MouseState currentState, MouseState previousState)
		{
			foreach (var namedControl in mControls)
			{
				namedControl.Value.CheckForEvents(currentState, previousState);
			}
		}

		/// <summary>
		/// Draws the screen with the specified SpriteBatch. SpriteBatch.Begin must have been called prior to calling this method.
		/// </summary>
		/// <param name="spriteBatch">The SpriteBatch to draw with.</param>
		public void Draw(SpriteBatch spriteBatch)
		{
			foreach(var namedControl in mControls)
			{
				if (namedControl.Value.IsVisible)
				{
					namedControl.Value.Draw(spriteBatch);
				}
			}
		}
	}
}
